package com.aeon.jface.tableViewer.ui;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class TableViewerComposit extends Composite {

	private TableViewer mTableViewer;

	private Composite mTableComposite;
	private Composite mFilterComposite;

	private MFilter mFilter;
	public TableViewerComposit(Composite parent, int style) {
		super(parent, style);
		init();
	}

	private void init() {
		
		RowLayout layout= new RowLayout(SWT.VERTICAL);
		layout.fill=true;
		setLayout(layout);
		
		mFilter=new MFilter();
		
		createFilterComposite();

		
		createTableComposite();
		mTableViewer.setFilters(new MFilter[]{mFilter});

	}

	private void createFilterComposite() {
		mFilterComposite = new Composite(this, SWT.BORDER);
		mFilterComposite.setLayout(new FillLayout());
		final Text filterText = new Text(mFilterComposite, SWT.NONE);

		filterText.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				mFilter.setRegex(filterText.getText());
				mTableViewer.refresh();
			}
		});
	}

	private void createTableComposite() {

		mTableComposite = new Composite(this, SWT.NONE);

		mTableViewer = new TableViewer(mTableComposite, SWT.FULL_SELECTION
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		mTableViewer.getTable().setHeaderVisible(true);
		mTableViewer.getTable().setLinesVisible(true);

		mTableViewer.setContentProvider(new TableViewerContentProvider());

		TableViewerColumn nameColumn = addColumns(mTableViewer, "Name");
		nameColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof SymbolDescriptor) {
					SymbolDescriptor symbolDescriptor = (SymbolDescriptor) element;

					return symbolDescriptor.getImageName();
				}
				return null;
			}

		});

		TableViewerColumn descColumn = addColumns(mTableViewer, "Description");
		descColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof SymbolDescriptor) {
					SymbolDescriptor symbolDescriptor = (SymbolDescriptor) element;

					return symbolDescriptor.getDescription();
				}
				return null;
			}

		});

		TableViewerColumn pathColumn = addColumns(mTableViewer, "Path");
		pathColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof SymbolDescriptor) {
					SymbolDescriptor symbolDescriptor = (SymbolDescriptor) element;

					return symbolDescriptor.getPath();
				}
				return null;
			}

		});

		MTableModel model = prepareModel();

		// mTableViewer.setLabelProvider(new TableViewerLabelProvider());

		TableColumnLayout layout = new TableColumnLayout();

		layout.setColumnData(nameColumn.getColumn(), new ColumnWeightData(30,
				100, true));
		layout.setColumnData(descColumn.getColumn(), new ColumnWeightData(30,
				100, true));
		layout.setColumnData(pathColumn.getColumn(), new ColumnWeightData(40,
				100, true));

		mTableComposite.setLayout(layout);

		mTableViewer.setInput(model);

	}

	private TableViewerColumn addColumns(TableViewer mTableViewer2,
			String header) {

		TableViewerColumn column = new TableViewerColumn(mTableViewer2,
				SWT.None);
		column.getColumn().setText(header);
		column.getColumn().setMoveable(true);
		column.getColumn().setWidth(50);
		return column;
	}

	private MTableModel prepareModel() {
		MTableModel model = new MTableModel();

		for (int i = 0; i < 20; i++) {
			model.add(new SymbolDescriptor("name " + i, "path " + i, "desc "
					+ i));
		}

		return model;
	}

}
